const crypto = require('crypto');
const request = require('request');

//请将 appId 修改为你的 appId，appid 为 number
var appId = 1048172193;
//请将 serverSecret 修改为你的 serverSecret，serverSecret 为 string
var serverSecret = "b86af07b8a280de3ed39fe2ab30b9b8f";

//Signature=md5(AppId + SignatureNonce + ServerSecret + Timestamp)
function GenerateUASignature(appId, signatureNonce, serverSecret, timeStamp) {
  const hash = crypto.createHash('md5'); //规定使用哈希算法中的MD5算法
  var str = appId + signatureNonce + serverSecret + timeStamp;
  hash.update(str);
  //hash.digest('hex')表示输出的格式为16进制
  return hash.digest('hex');
}

var signatureNonce = crypto.randomBytes(8).toString('hex');
var timeStamp = Math.round(Date.now() / 1000);
var sig = GenerateUASignature(appId, signatureNonce, serverSecret, timeStamp)

//以下示例为 将 streamID 为 "stream1" 的音视频流 与 streamID 为 "stream2" 的音视频流进行混流，混出 streamID 为 "stream3" 的音视频流
var url = `https://rtc-api.zego.im/?Action=StartMix&AppId=${appId}&SignatureNonce=${signatureNonce}&Timestamp=${timeStamp}&Signature=${sig}&SignatureVersion=2.0&IsTest=false`
var body = {
  'TaskId': '123',
  'Sequence': 123,
  'UserId': '123',
  'MixInput': [
    {
      'StreamId': 'stream1',
      'RectInfo': {
        "Top": 70,
        "Bottom": 160,
        "Left": 100,
        "Right": 260,
      },

    }, 
    {
      'StreamId': 'stream2',
      'RectInfo': {
        "Top": 200,
        "Bottom": 290,
        "Left": 100,
        "Right": 260,
      },
    }
  ],
  'MixOutput': [{
    'StreamId': 'stream3',
    'Width': 360,
    'Height': 360,
    'VideoBitrate': 12000,
    'Fps': 15
  }]
}

request({
  url: url,
  method: 'POST',
  json: true,
  headers: {
    'content-type': 'application/json'
  },
  body: body
}, function (error, response, body) {
  console.log('url:',url)
  console.log('statusCode: ',response.statusCode)
  console.log('error:', error)
  if (!error && response.statusCode === 200) {
    console.log(body.Data)
  }
  
})